#include "redismgr.h"

RedisMgr::RedisMgr(QObject *parent) : QObject(parent)
{

}

RedisMgr &RedisMgr::getInstance()
{
    static RedisMgr instance;
    return instance;
}

bool RedisMgr::Connect(const std::string& host, int port)
{
    this->_connect = redisConnect(host.c_str(), port);
    if (this->_connect != NULL && this->_connect->err)
    {
        std::cout << "connect error " << this->_connect->errstr << std::endl;
        return false;
    }
    return true;
}

bool RedisMgr::Auth(const std::string& password)
{
    this->_reply = (redisReply*)redisCommand(this->_connect, "AUTH %s", password.c_str());
    if (this->_reply->type == REDIS_REPLY_ERROR) {
        std::cout << "认证失败" << std::endl;
        //执行成功 释放redisCommand执行后返回的redisReply所占用的内存
        freeReplyObject(this->_reply);
        return false;
    }
    else {
        //执行成功 释放redisCommand执行后返回的redisReply所占用的内存
        freeReplyObject(this->_reply);
        std::cout << "认证成功" << std::endl;
        return true;
    }
}

bool RedisMgr::SAdd(const std::string &key, const std::string &value)
{
    this->_reply = (redisReply*)redisCommand(this->_connect, "sadd %s %s", key.c_str(), value.c_str());
    if (_reply == nullptr || _reply->type != REDIS_REPLY_INTEGER) {
        std::cout << "Execut command [ sadd " << key << "  " << value << " ] failure ! " << std::endl;
        freeReplyObject(this->_reply);
        return false;
    }
    std::cout << "Execut command [ sadd " << key << "  " << value << " ] success ! " << std::endl;
    freeReplyObject(this->_reply);
    return true;
}

bool RedisMgr::SREM(const std::string &key, const std::string &value)
{
    this->_reply = (redisReply*)redisCommand(this->_connect, "srem %s %s", key.c_str(), value.c_str());
    if (_reply == nullptr || _reply->type != REDIS_REPLY_INTEGER) {
        std::cout << "Execut command [ srem " << key << "  " << value << " ] failure ! " << std::endl;
        freeReplyObject(this->_reply);
        return false;
    }
    std::cout << "Execut command [ srem " << key << "  " << value << " ] success ! " << std::endl;
    freeReplyObject(this->_reply);
    return true;
}

QStringList RedisMgr::SMember(const std::string &key)
{
    QStringList resultList;
    resultList.clear();
    this->_reply = (redisReply*)redisCommand(this->_connect, "SMEMBERS %s", key.c_str());
    if (_reply == nullptr) {
        std::cout << "Execut command [ SMEMBERS " << key << " ] failure ! " << std::endl;
        freeReplyObject(this->_reply);
        return resultList;
    }
    std::cout << "Execut command [ SMEMBERS " << key << " ] success ! " << std::endl;
    for(int i = 0; i < _reply->elements; i++)
    {
        resultList.append(QString(_reply->element[i]->str));
    }
    freeReplyObject(this->_reply);
    return resultList;
}


bool RedisMgr::Del(const std::string& key)
{
    this->_reply = (redisReply*)redisCommand(this->_connect, "DEL %s", key.c_str());
    if (this->_reply == nullptr || this->_reply->type != REDIS_REPLY_INTEGER) {
        std::cout << "Execut command [ Del " << key << " ] failure ! " << std::endl;
        freeReplyObject(this->_reply);
        return false;
    }
    std::cout << "Execut command [ Del " << key << " ] success ! " << std::endl;
    freeReplyObject(this->_reply);
    return true;
}
